package com.quanshu.shop.order.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.shop.order.entity.SubmitPrepareEntity;

import java.math.BigDecimal;
import java.util.List;

/**
 * 作者  李哲
 * 创建时间 2018/8/1
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class SubmitOrderAdapter extends BaseQuickAdapter<SubmitPrepareEntity.GoodsListEntity, BaseViewHolder> {
    private Context mContext;
    private GetEditWordsListenner getEditWordsListenner;

    public SubmitOrderAdapter(@Nullable List<SubmitPrepareEntity.GoodsListEntity> data, Context context1, GetEditWordsListenner getEditWordsListenner) {
        super(R.layout.item_submit_order, data);
        mContext = context1;
        this.getEditWordsListenner = getEditWordsListenner;
    }

    @Override
    protected void convert(BaseViewHolder helper, SubmitPrepareEntity.GoodsListEntity item) {
        Glide.with(mContext).load(Config.URL_HOST_SHOP + item.getImg()).into((ImageView) helper.getView(R.id.item_order_img));
        helper.setText(R.id.item_order_goods_title, item.getName());
        BigDecimal bigDecimalNumber = new BigDecimal(item.getCount());
        BigDecimal bigDecimalPresent = new BigDecimal("2");
        helper.setText(R.id.item_order_goods_number, "X" + item.getCount());
        if (Integer.parseInt(item.getPayment_options()) == 0) {
            //现金支付
            helper.setVisible(R.id.item_order_goods_njf, false);
            helper.setVisible(R.id.item_order_transfer_njf, false);
            helper.setVisible(R.id.item_order_buy_njf, false);
            helper.setVisible(R.id.item_order_goods_money, true);
            helper.setVisible(R.id.item_order_buy_money, true);
            helper.setVisible(R.id.item_order_transfer_money, true);
            BigDecimal bigDecimalMoney = new BigDecimal(item.getSell_price());
            if (Integer.parseInt(item.getIs_service()) == 0) {
                //没有大礼包
                helper.setText(R.id.item_order_buy_money, bigDecimalMoney.multiply(bigDecimalNumber).toString());
                helper.setText(R.id.item_order_transfer_money, item.getFreight());
                helper.setText(R.id.item_order_goods_money, item.getSell_price());
            } else if (Integer.parseInt(item.getIs_service()) == 1) {
                helper.setText(R.id.item_order_buy_money, bigDecimalMoney.multiply(bigDecimalNumber).multiply(bigDecimalPresent).toString());
                helper.setText(R.id.item_order_transfer_money, item.getFreight());
                helper.setText(R.id.item_order_goods_money, bigDecimalMoney.multiply(bigDecimalPresent).toString());
            }

        } else if (Integer.parseInt(item.getPayment_options()) == 1) {
            //牛积分支付
            helper.setVisible(R.id.item_order_goods_njf, true);
            helper.setVisible(R.id.item_order_transfer_njf, true);
            helper.setVisible(R.id.item_order_buy_njf, true);
            helper.setVisible(R.id.item_order_goods_money, false);
            helper.setVisible(R.id.item_order_buy_money, false);
            helper.setVisible(R.id.item_order_transfer_money, false);
            BigDecimal bigDecimalNjf = new BigDecimal(item.getNiu_score());
            if (Integer.parseInt(item.getIs_service()) == 0) {
                helper.setText(R.id.item_order_buy_njf, bigDecimalNjf.multiply(bigDecimalNumber).toString());
                helper.setText(R.id.item_order_transfer_njf, item.getFreight());
                helper.setText(R.id.item_order_goods_njf, item.getNiu_score());
            } else if (Integer.parseInt(item.getIs_service()) == 1) {
                helper.setText(R.id.item_order_buy_njf, bigDecimalNjf.multiply(bigDecimalNumber).multiply(bigDecimalPresent).toString());
                helper.setText(R.id.item_order_transfer_njf, item.getFreight());
                helper.setText(R.id.item_order_goods_njf, bigDecimalNjf.multiply(bigDecimalPresent).toString());
            }

        }
        helper.setText(R.id.item_order_words_edt, item.getWords());
        EditText editText = helper.getView(R.id.item_order_words_edt);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                    if (getEditWordsListenner!=null){
                        getEditWordsListenner.getEditData(s.toString(),helper.getLayoutPosition());
                    }
            }
        });

    }

    public interface GetEditWordsListenner {
        void getEditData(String s, int position);
    }
}
